#-*- coding:UTF-8 -*-

import httplib
import MySQLdb
import time
import Config
import util

db=MySQLdb.connect(host=Config.DBHOST,
                   user=Config.DBUSER,passwd=Config.DBPASS,db=Config.DB,
                   connect_timeout=3600,charset="utf8",use_unicode=True)

host=Config.HOST

def UploadAlbum(dbconn):
    i=0
    headers={"Content-Type":"application/x-www-form-urlencoded"}
    cur=dbconn.cursor()
    cur.execute("SELECT id,name,picnum FROM album WHERE  id=51")
    rs=cur.fetchall()
    for r in rs:
        i+=1
        print i,
        retry=True
        while retry:
            retry=False
            try:
                name=r[1]
                cur.execute("SELECT image FROM picture WHERE albumid=%s LIMIT 1",r[0])
                pic=cur.fetchone()
                if pic:
                    pic=pic[0]
                else:
                    pic="XXXX"
                para=[]
                para.append(("name",name.encode("utf8")))
                para.append(("ownerid","1"))
                para.append(("picnum",str(r[2])))
                resp=util.post_multipart(host,"/album/create",para,[("cover","tmp",pic)])
                
            except Exception,e:
                print "Exception: ",e
                retry=True
                time.sleep(10)

if __name__=="__main__":
    UploadAlbum(db)